package com.cby.android.sample.activities

import android.text.TextUtils
import android.widget.Button
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cby.android.library.base.BaseStateActivity
import com.cby.android.library.base.callback.EmptyCallback
import com.cby.android.library.base.callback.ErrorCallback
import com.cby.android.library.base.callback.LottieLoadingCallback
import com.cby.android.library.base.callback.TimeoutCallback
import com.cby.android.library.ext.observe
import com.cby.android.library.utils.KeyboardUtils
import com.cby.android.library.widget.edittext.MClearEditText
import com.cby.android.library.widget.snackbar.MySnackbar
import com.cby.android.sample.R
import com.cby.android.sample.adapter.RollCommonQueryAdapter
import com.cby.android.sample.bean.roll.RollCommonBean
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.model.RollMoreViewModel
import com.cby.android.sample.state.RollMoreState
import com.kingja.loadsir.core.LoadSir
import kotlinx.coroutines.launch

/**
 * @description: RollDictionaryActivity
 *
 * @author ChenBaoyang
 * @date 4/25/21 10:58
 */
class RollDictionaryActivity : BaseStateActivity<RollMoreViewModel, RollMoreState, RollMoreIntent>() {

    private val queryBtn: Button by lazy { findViewById(R.id.btn_query) }
    private val editText: MClearEditText by lazy { findViewById(R.id.editText) }
    private val recyclerView: RecyclerView by lazy { findViewById(R.id.recyclerView) }

    private lateinit var queryAdapter: RollCommonQueryAdapter

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            val datas = mutableListOf<RollCommonBean>()
            datas.add(RollCommonBean("原内容:", it.dataDictionary[0].word))
            datas.add(RollCommonBean("繁体:", it.dataDictionary[0].traditional))
            datas.add(RollCommonBean("拼音:", it.dataDictionary[0].pinyin))
            datas.add(RollCommonBean("偏旁部首:", it.dataDictionary[0].radicals))
            datas.add(RollCommonBean("汉字释义:", it.dataDictionary[0].explanation))
            datas.add(RollCommonBean("汉字笔画数:", "${it.dataDictionary[0].strokes}"))
            queryAdapter.setNewData(datas)
        }
    }

    override fun getTargetView() = recyclerView

    override fun onViewInit() {

        title = "新华字典"

        queryAdapter = RollCommonQueryAdapter(R.layout.roll_item_common_query)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = queryAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()

        queryBtn.setOnClickListener {
            KeyboardUtils.hideInputKeyboard(this)

            val word: String = editText.getText().toString()

            if (TextUtils.isEmpty(word)) {
                MySnackbar.makeSnackBarRed(toolbar, "查询内容不能为空")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewModel!!.intents.send(RollMoreIntent.LoadRollDictionaryIntent(word))
            }
        }
    }

    override fun initLoadSir() {
        val loadSir = LoadSir.Builder()
            .addCallback(LottieLoadingCallback())
            .addCallback(EmptyCallback())
            .addCallback(ErrorCallback())
            .addCallback(TimeoutCallback())
            .setDefaultCallback(EmptyCallback::class.java)
            .build()
        loadService = loadSir.register(getTargetView()){
            reload()
        }
    }

    override fun reload() {
    }

    override fun getLayoutId() = R.layout.roll_activity_dictionary

}